package net.huashitong.common.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysMessage;
import net.huashitong.common.enums.MsgStatusEnum;
import net.huashitong.common.filter.SysMessageFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author
 */
@Repository
public class SysMessageDao extends StringDao<SysMessage> {

    public List<SysMessage> queryByFilter(SysMessageFilter filter){
        GenericQuery query = createQuery("t");
        if(StringUtils.isNotBlank(filter.getEsReceiver())) {
            query.append(" AND t.esReceiver =:esReceiver").setParam("esReceiver",filter.getEsReceiver());
        }
        if(StringUtils.isNotBlank(filter.getEsSendStatus())) {
            query.append(" AND t.esSendStatus =:esSendStatus").setParam("esSendStatus",filter.getEsSendStatus());
        }
        if(StringUtils.isNotBlank(filter.getEsType())) {
            query.append(" AND t.esType =:esType").setParam("esType",filter.getEsType());
        }
        if(StringUtils.isNotBlank(filter.getEsTitle())) {
            query.append(" AND t.esTitle like :esTitle").setParam("esTitle",StringUtils.getLikewords(filter.getEsTitle()));
        }
        if(StringUtils.isNotBlank(filter.getEsContent())) {
            query.append(" AND t.esContent like :esContent").setParam("esContent",StringUtils.getLikewords(filter.getEsContent()));
        }
        if (StringUtils.isNotBlank(filter.getSendTimeFrom())) {
            query.append(" AND t.esSendTime > :esSendTime").setParam("esSendTime",filter.getSendTimeFrom());
        }
        if (StringUtils.isNotBlank(filter.getSendTimeTo())) {
            query.append(" AND t.esSendTime < :esSendTime").setParam("esSendTime",filter.getSendTimeTo());
        }
        return query.listResult(filter);
    }

    public List<SysMessage> querySendList(){
        GenericQuery query = createQuery("t");
        //查询等待发送和失败6次以内的消息
        query.append(" AND t.esSendStatus = :esSendStatus").setParam("esSendStatus", MsgStatusEnum.WAIT.getCode());
        query.append(" OR (t.esSendStatus = :esSendStatus").setParam("esSendStatus",MsgStatusEnum.FAIL.getCode());
        query.append(" t.esSendNum< :esSendNum)").setParam("esSendNum",6);
        return query.listResult();
    }
}
